package cn.iocoder.yudao.module.resource.service.points;

import cn.iocoder.yudao.module.resource.dal.dataobject.points.ResourcePointsRecordDO;
import cn.iocoder.yudao.module.resource.dal.mysql.points.ResourcePointsRecordMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

/**
 * 积分记录 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
@Slf4j
public class ResourcePointsServiceImpl implements ResourcePointsService {

    @Resource
    private ResourcePointsRecordMapper pointsRecordMapper;

    @Override
    public Long createPointsRecord(ResourcePointsRecordDO pointsRecordDO) {
        pointsRecordMapper.insert(pointsRecordDO);
        return pointsRecordDO.getId();
    }

    @Override
    public ResourcePointsRecordDO getPointsRecord(Long id) {
        return pointsRecordMapper.selectById(id);
    }

    @Override
    public List<ResourcePointsRecordDO> getPointsRecordListByCreatorId(Long creatorId) {
        return pointsRecordMapper.selectListByCreatorId(creatorId);
    }

    @Override
    public Integer calculateTotalPoints(Long creatorId) {
        List<ResourcePointsRecordDO> records = getPointsRecordListByCreatorId(creatorId);
        return records.stream()
                .mapToInt(ResourcePointsRecordDO::getPoints)
                .sum();
    }

}
